package booking.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.Timestamp;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.border.EmptyBorder;

import booking.misc.Airport;
import booking.misc.Flight;
public class FlightDialog extends JDialog implements ActionListener {
    
    private static final String OK = "ok";
    private static final String CANCEL = "cancel";
    
    Flight    flight = null;
    JComboBox<Airport> depatureairportIdBox;
    JComboBox<Airport> arrivalairportIdBox;
    JTextField todFld;
    JTextField priceFld;
    JSpinner spinnerDate;
    public FlightDialog(JFrame frame, Flight flight) {
        this(frame);
        this.flight = flight;
    }
    
    public FlightDialog(JFrame frame) {
        setTitle("Add Flight");
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(frame);
        JPanel dataPnl = new JPanel(new GridLayout(6, 2, 5, 5));
        dataPnl.setBorder(new EmptyBorder(5, 5, 5, 5));
        add(dataPnl, BorderLayout.CENTER);
        
        dataPnl.add(new JLabel("Depature Airport:"));
        depatureairportIdBox = new JComboBox<Airport>(Airport.loadAirports());
        dataPnl.add(depatureairportIdBox);
        
        dataPnl.add(new JLabel("Arrival Airport:"));
        arrivalairportIdBox = new JComboBox<Airport>(Airport.loadAirports());
        dataPnl.add(arrivalairportIdBox);

        dataPnl.add(new JLabel("Time of Depature:"));
        //todFld = new JTextField(10);
        spinnerDate = new JSpinner(new SpinnerDateModel());
        dataPnl.add(spinnerDate);
        
        dataPnl.add(new JLabel("Price:"));
        priceFld = new JTextField(10);
        dataPnl.add(priceFld);
        
        JPanel bottomPnl = new JPanel();
        add(bottomPnl, BorderLayout.PAGE_END);
        
        JButton btn = new JButton("OK");
        btn.setActionCommand(OK);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        btn = new JButton("Cancel");
        btn.setActionCommand(CANCEL);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        
        pack();
    }
    
    public Flight getFlight() {
        return flight;
    }
    public void actionPerformed(ActionEvent event) {
        String ac = event.getActionCommand();
        if (ac.equals(OK)) {
        	
        	Airport da = (Airport) depatureairportIdBox.getSelectedItem();
        	Airport aa = (Airport) arrivalairportIdBox.getSelectedItem();
        	
        	SpinnerDateModel dateModel = (SpinnerDateModel) spinnerDate.getModel();
        	Timestamp date = new Timestamp(dateModel.getDate().getTime());    
        	
        	double price;
        	try {
            price = Double.parseDouble(priceFld.getText());
        	} catch (NumberFormatException e){
        		JOptionPane.showMessageDialog(this, "Fel pris");
        		return;
        	} 
        	
        	
            if (flight == null)
            	flight = new Flight(-1, da.getAirportId(), aa.getAirportId(), date, price);
            else {
            	flight.setDepatureairportId(da.getAirportId());
            	flight.setArrivalairportId(aa.getAirportId());
            	flight.setTod(date);
            	flight.setFlightPrice(price);
            }
            dispose();
        }
        else if (ac.equals(CANCEL)) {
        	flight = null;
            dispose();
        }
    }
}

